// pages/kind/kind.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    kindTitleList: [],
    num: 0,
    title: '',
    kindTitleNum: []
  },
  async addNum() {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      num: this.data.num + 1
    })
    console.log(this.data.num)
    await setTimeout(() => {
      this.getKindList()
    }, 800)
  },
  jumpGo(e) {
    // console.log(e.currentTarget.dataset.tag)
    wx.setStorageSync('tag', e.currentTarget.dataset.tag)
    wx.navigateTo({
      url: '../kindList/kindList',
    })
  },
  // http://localhost:3000/poetry/tagTagList
  // tagTagList(){

  //   wx.request({
  //     url: 'http://localhost:3000/poetry/tagTagList',
  //     method:"POST",
  //     data:{

  //     }
  //   })
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  getKindNum(x, y) {
    let that = this
    wx.request({
      url: 'http://localhost:3000/poetry/tagTagListNum',
      method: "POST",
      data: {
        name: x
      },
      success(data) {
        console.log(data.data.row[0]['count(*)'])
        let newArr = that.data.kindTitleList;
        // if (newArr.length == 5) {
        //   newArr = []
        // }
        newArr[y]["num"] = data.data.row[0]['count(*)']
        that.setData({
          kindTitleList: newArr
        })
      }
    })
  },
  getKindList() {
    let num = this.data.num
    let that = this
    return new Promise((reslove, reject) => {
      wx.request({
        url: 'http://localhost:3000/poetry/tagListLimit',
        method: "POST",
        data: {
          num: num % 11
        },
        success(data) {
          that.setData({
            kindTitleList: data.data.rows
          })
          data.data.rows.map((item, index) => {
            console.log(item.name)
            that.getKindNum(item.name, index)
          })

          wx.hideLoading()
          reslove({
            code: 200,
            msg: "获取成功"
          })
        }
      })
    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    wx.showLoading({
      title: '加载中',
    })
    await this.getKindList()
    wx.hideLoading()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})